﻿using System;
using System.Windows.Input;

using TrueCode.Core;
using TrueCode.Core.Aspects;

namespace EMan.BL.Commands
{
  public class SimpleCommand<T> : ICommand
  {
    private readonly Action< T > _execute;
    private bool _canExecute;

    public SimpleCommand( [NotNull] Action< T > execute, bool canExecute = true )
    {
      if( execute == null ) throw new ArgumentNullException( "execute" );
      _execute = execute;
      _canExecute = canExecute;
    }

    public void Execute( object parameter )
    {
      _execute( ( T )parameter );
    }

    bool ICommand.CanExecute( object parameter )
    {
      return CanExecute;
    }

    public bool CanExecute
    {
      get { return _canExecute; }
      set
      {
        if(_canExecute != value)
        {
          _canExecute = value;
          CanExecuteChanged.Raise( this );
        }
      }
    }

    public event EventHandler CanExecuteChanged;
  }
}